## Function to export rd.robust table:
require(xtable)
rd.export.one <- function(rd.fit){
  out.table <- round(cbind(rd.fit$coef[1],rd.fit$se[3], rd.fit$z[3], rd.fit$pv[3], rd.fit$ci[3]),3)
  dv <- paste(rd.fit$call)[2]
  print(xtable(cbind(dv,out.table)))
}
# for multiple models:
rd.export <- function(rd.fits){
  if(length(rd.fits)==1){
    rd.fit <- rd.fits
    out.table <- round(cbind(rd.fit$coef[1],rd.fit$se[3], rd.fit$z[3], rd.fit$pv[3], rd.fit$ci[3]),3)
    dv <- paste("DV = ",paste(rd.fit$call)[2],sep="")
    output <- (xtable(cbind(dv,out.table)))
  }
  if(length(rd.fits)>1){
    output <- NULL
    for(i in 1:length(rd.fits)){
      rd.fit <- unlist(rd.fits[i])
      out.table <- round(cbind(rd.fit$coef1,rd.fit$se3,rd.fit$z3,rd.fit$pv3,rd.fit$ci3,rd.fit$ci6),3)
      dv <- paste(rd.fit$call)[2]
      out.table <- cbind(dv,out.table)
      colnames(out.table) <- c("DV","Coef","Std.Err.","z","p-value","CI Lower","CI Upper")
      thisoutput <- (xtable(out.table))
      output <- (rbind(output,thisoutput))
    }
  }
  print((output))
}
##

# for multiple models, paper-formatted version of export:
rd.export2 <- function(rd.fits){
  if(length(rd.fits)==1){
    rd.fit <- rd.fits
    out.table <- round(cbind((100*rd.fit$h),rd.fit$coef[1], rd.fit$ci[3], rd.fit$pv[3]),3)
    dv <- paste("DV = ",paste(rd.fit$call)[2],sep="")
    output <- (xtable(cbind(dv,out.table)))
  }
  if(length(rd.fits)>1){
    output <- NULL
    for(i in 1:length(rd.fits)){
      rd.fit <- unlist(rd.fits[i])
      out.table <- round(cbind((100*rd.fit$h),rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3),2)
      out.table <- cbind(paste(rd.fit$call)[2],out.table)
      out.table <- cbind(out.table[,1],out.table[,2],out.table[,3],paste("(",out.table[,4],", ",out.table[,5],")",sep=""),out.table[,6])
      colnames(out.table) <- c("DV","BW","Coef","CI","p-value")
      thisoutput <- (xtable(out.table))
      output <- (rbind(output,thisoutput))
    }
  }
  print((output))
}

# for multiple models, paper-formatted version of export:
rd.export3 <- function(rd.fits){
    if(length(rd.fits)==1){
        rd.fit <- unlist(rd.fits[[1]])
        out.table <- round(cbind(rd.fit$coef1, rd.fit$ci3,rd.fit$ci6, rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$h)),3)
        dv <- paste("DV = ",paste(rd.fit$call)[2],sep="")
        output <- (xtable(cbind(dv,out.table)))
    }
    if(length(rd.fits)>1){
        output <- NULL
        for(i in 1:length(rd.fits)){
            rd.fit <- unlist(rd.fits[i])
            out.table <- round(cbind(rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$h)),2)
            out.table <- cbind(paste(rd.fit$call)[2],out.table)
            out.table <- cbind(out.table[,1],out.table[,2],paste("(",out.table[,3],",",out.table[,4],")",sep=""),out.table[,5],out.table[,6],out.table[,7])
            colnames(out.table) <- c("DV","Coef","CI","p-value","Obs","BW")
            thisoutput <- (xtable(out.table))
            output <- (rbind(output,thisoutput))
            # print((out.table))

        }
    }
    print((output))
}

# for multiple models, paper-formatted version of export:
rd.export4 <- function(rd.fits){
  if(length(rd.fits)==1){
    rd.fit <- unlist(rd.fits[[1]])
    out.table <- round(cbind(rd.fit$coef1, rd.fit$ci3,rd.fit$ci6, rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$h)),3)
    dv <- paste("DV = ",paste(rd.fit$call)[2],sep="")
    output <- (xtable(cbind(dv,out.table)))
  }
  if(length(rd.fits)>1){
    output <- NULL
    for(i in 1:length(rd.fits)){
      rd.fit <- unlist(rd.fits[i])
      out.table.initial <- round(cbind(rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$h)),2)
      out.table.initial2 <- round(cbind(rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$h)),1)
      out.table <- cbind(paste(rd.fit$call)[2],out.table.initial)
      out.table <- cbind(out.table[,1],out.table[,2],out.table[,5],out.table[,6],out.table[,7])
      out.table2 <- cbind("", paste("(",out.table.initial[,2],", ",out.table.initial[,3],")",sep=""),"","","")
      colnames(out.table) <- c("DV","Coef","p-value","BW","Obs")
      colnames(out.table2) <- c("DV","Coef","p-value","BW","Obs")
      thisoutput <- (xtable(out.table))
      thisoutput2 <- (xtable(out.table2))
      output <- (rbind(output,thisoutput,thisoutput2))
      # print((out.table))
      
    }
  }
  print((output))
}

### For new version of rdrobust, post-2016:
rd.export5 <- function(rd.fits){
  if(length(rd.fits)==1){
    rd.fit <- unlist(rd.fits[[1]])
    out.table <- round(cbind(rd.fit$coef1, rd.fit$ci3,rd.fit$ci6, rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$h)),2)
    dv <- paste("DV = ",paste(rd.fit$call)[2],sep="")
    output <- (xtable(cbind(dv,out.table)))
  }
  if(length(rd.fits)>1){
    output <- NULL
    for(i in 1:length(rd.fits)){
      rd.fit <- unlist(rd.fits[i])
      out.table.initial <- round(cbind(rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$bws1)),2)
      out.table.initial2 <- round(cbind(rd.fit$coef1,rd.fit$ci3,rd.fit$ci6,rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$bws1)),1)
      out.table <- cbind(paste(rd.fit$call)[2],out.table.initial)
      out.table <- cbind(out.table[,1],out.table[,2],out.table[,5],out.table[,6],out.table[,7])
      out.table2 <- cbind("", paste("(",out.table.initial[,2],", ",out.table.initial[,3],")",sep=""),"","","")
      colnames(out.table) <- c("DV","Coef","p-value","Obs","BW")
      colnames(out.table2) <- c("DV","Coef","p-value","Obs","BW")
      thisoutput <- (xtable(out.table))
      thisoutput2 <- (xtable(out.table2))
      output <- (rbind(output,thisoutput,thisoutput2))
      # print((out.table))
      
    }
  }
  print((output))
}

# for multiple models, paper-formatted version of export:
rd.export.shares<- function(rd.fits){
    if(length(rd.fits)==1){
        rd.fit <- unlist(rd.fits[[1]])
        out.table <- round(cbind(rd.fit$coef1*100, rd.fit$ci3*100,rd.fit$ci6*100, rd.fit$pv3,as.numeric(rd.fit$tabl1.str1),(100*rd.fit$h)),3)
        dv <- paste("DV = ",paste(rd.fit$call)[2],sep="")
        output <- (xtable(cbind(dv,out.table)))
    }
    if(length(rd.fits)>1){
        output <- NULL
        for(i in 1:length(rd.fits)){
            rd.fit <- unlist(rd.fits[i])
            out.table.initial <- round(cbind(rd.fit$coef1*100,rd.fit$ci3*100,rd.fit$ci6*100,rd.fit$pv3,(100*rd.fit$h),as.numeric(rd.fit$tabl1.str1)),2)
            out.table.initial2 <- round(cbind(rd.fit$coef1*100,rd.fit$ci3*100,rd.fit$ci6*100,rd.fit$pv3,(100*rd.fit$h),as.numeric(rd.fit$tabl1.str1)),2)
            out.table <- cbind(paste(rd.fit$call)[2],out.table.initial)
            out.table <- cbind(out.table[,1],out.table[,2],out.table[,5],out.table[,6],out.table[,7])
             out.table2 <- cbind("", paste("(",out.table.initial2[,2],", ",out.table.initial2[,3],")",sep=""),"","","")
            colnames(out.table) <- c("DV","Coef","p-value","BW","Obs")
               colnames(out.table2) <- c("DV","Coef","p-value","BW","Obs")
         thisoutput <- (xtable(out.table))
             thisoutput2 <- (xtable(out.table2))
           output <- (rbind(output,thisoutput,thisoutput2))
            # print((out.table))

        }
    }
    print((output))
}
